package com.dd.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.nutz.castor.Castors;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.tio.websocket.common.util.Md5;

import com.dd.dao.ChatMessageDao;
import com.dd.dao.UserDao;
import com.dd.entity.Flock;
import com.dd.entity.FlockRefUser;
import com.dd.entity.Friends;
import com.dd.entity.Group;
import com.dd.entity.InitData;
import com.dd.entity.JsonMsgModel;
import com.dd.entity.Message;
import com.dd.entity.RcUser;
import com.dd.entity.User;
import com.dd.mvc.Response;
import com.dd.socket.Type;
import com.dd.utils.SocketMsgUtils;
import com.dd.utils.SocketUtils;

/**
 * 主控制器
 * 
 * @Author: 蛋蛋 [TopCoderMyDream@gmail.com,https://github.com/TopCoderMyDream]
 * @Time:2017/12/12 13:54
 */
@At("/user")
@IocBean
@Ok("json")
public class UserController {
	@Inject(value = "userDao")
	private UserDao userDao;

	@Inject
	private Dao dao;

	@Inject
	private ChatMessageDao chatMessageDao;

	/**
	 * 登陆
	 * 
	 * @param u
	 * @param session
	 * @return
	 */
	@At
	@POST
	public Object login(@Param("..") User u, HttpSession session) {

		String msg = checkUser(u, false);
		if (msg != null) {
			return Response.fail(msg);
		}

		UsernamePasswordToken token = new UsernamePasswordToken(u.getUsername(), u.getPwd());
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);//
		} catch (UnknownAccountException e2) {
			return Response.fail("账户不存在!");
		} catch (IncorrectCredentialsException e1) {
			return Response.fail("密码错误!");
		}
		User fetch = dao.fetch(User.class, Cnd.where(User.USERNAME, "=", u.getUsername()));
		if (SecurityUtils.getSubject().isAuthenticated()) {

			subject.getSession().setAttribute("me", fetch.getId());
			subject.getSession().setAttribute("username", fetch.getUsername());
			subject.getSession().setAttribute("sessionId", session.getId());

			return Response.ok("登陆成功!");
		} else {
			return Response.fail("登录失败!");
		}

	}

	/**
	 * 注册
	 * 
	 * @param user
	 * @param session
	 * @return
	 */
	@At
	@POST
	public Object registry(@Param("..") User user, HttpSession session) {
		String msg = checkUser(user, true);
		if (msg != null) {
			return Response.fail().setv("ok", false).setv("msg", msg);
		}

		String avatar = getAvatar();
		user.setAvatar("imgs/" + avatar + ".jpg");
		user.setCreate_time(new Timestamp(System.currentTimeMillis()));
		User u = userDao.save(user);
		if (u == null) {
			return Response.fail().setv("ok", false).setv("msg", "注册失败!");
		} else {
			UsernamePasswordToken token = new UsernamePasswordToken(u.getUsername(), u.getPwd());
			Subject subject = SecurityUtils.getSubject();
			try {
				subject.login(token);//
			} catch (UnknownAccountException e2) {
				return Response.fail("登录失败,请手动登录!");
			} catch (IncorrectCredentialsException e1) {
				return Response.fail("登录失败,请手动登录!");
			}

			if (SecurityUtils.getSubject().isAuthenticated()) {

				subject.getSession().setAttribute("me", u.getId());
				subject.getSession().setAttribute("username", u.getUsername());
				subject.getSession().setAttribute("sessionId", session.getId());

				/*
				 * //添加默认分组 userDao.addGroup(u.getId(), "亲人们"); int i =
				 * userDao.addGroup(u.getId(), "Ji友们");
				 * 
				 * //加群 FlockRefUser fr = new FlockRefUser(); fr.setFid(4);
				 * fr.setUid(user.getId()); dao.insert(fr);
				 * 
				 * //把我加上呀得 userDao.addFriend(user.getId(), 1, i);
				 * userDao.addFriend(1,user.getId(), 2);
				 */
				return Response.ok().setv("ok", true).setv("msg", "注册成功!");
			} else {
				return Response.ok().setv("ok", true).setv("msg", "自动登录失败,请手动登录!");
			}

		}
	}

	public String getAvatar() {
		int random = R.random(1, 15);
		return String.valueOf(random);
	}

	/**
	 * 查找用户
	 * 
	 * @param name
	 * @return
	 */
	@At
	public Object seachUser(@Param("name") String name) {
		List<User> users = userDao.getByLikeName(name);
		if (users != null) {
			for (int i = 0; i < users.size(); i++) {
				users.get(i).setPwd(null);
			}
		}
		return Json.toJson(users);
	}

	/**
	 * 查询用户和群组
	 * 
	 * @param name
	 * @return
	 */
	@At
	public Object seach(@Param("name") String name) {
		List<User> users = userDao.getByLikeName(name);
		List<Flock> flocks = dao.query(Flock.class, Cnd.where(Flock.GROUPNAME, "like", "%" + name + "%"));
		if (users != null) {
			for (int i = 0; i < users.size(); i++) {
				users.get(i).setPwd(null);
			}
		}
		return Response.ok().setv("users", Json.toJson(users)).setv("groups", Json.toJson(flocks));
	}

	/**
	 * 初始化数据
	 * 
	 * @param me
	 * @return
	 */
	@At
	@Ok("raw")
	public String getInitData(@Attr("me") int me) {
		String data = userDao.getInitData(me);
		return data;
	}

	/**
	 * 获取未读消息数量
	 * 
	 * @param me
	 * @return
	 */
	@At
	public Object unreadMsgCount(@Attr("me") int me) {
		List<Message> msgs = userDao.getUnMessages(me);
		int count = 0;
		if (msgs != null) {
			count = msgs.size();
		}
		NutMap nm = new NutMap();
		nm.setv("ok", true).setv("count", count);
		return nm;
	}

	/**
	 * 获取我的消息
	 * 
	 * @param me
	 * @return
	 */
	@At
	public Object getMsg(@Attr("me") int me) {
		List<Message> msgs = userDao.getMessages(me);
		JsonMsgModel jmm = new JsonMsgModel();
		jmm.setCode(0);
		jmm.setPages(1);
		jmm.setData(Castors.me().castTo(msgs, ArrayList.class));
		return jmm;
	}

	/**
	 * 已读我的消息
	 * 
	 * @param me
	 */
	@At
	public void markRead(@Attr("me") int me) {
		userDao.markRead(me);
	}

	/**
	 * 申请添加好友
	 * 
	 * @param me
	 *            我的id
	 * @param uid
	 *            对方id
	 * @param from_group
	 *            到哪个分组?
	 * @return
	 */
	@At
	public Object applyFriend(@Attr("me") int me, @Param("uid") int uid, @Param("from_group") int from_group,
			@Param("remark") String remark) {
		NutMap nm = new NutMap();
		if (!isFirend(me, uid)) {
			if (from_group == 0 || dao.fetch(Group.class, from_group) == null) {
				return Response.fail("请先添加分组");
			}
			int i = userDao.applyFriend(uid, me, from_group, remark);
			if (i > 0) {
				SocketUtils.sendByUserId(String.valueOf(uid), SocketMsgUtils.madeWsResponse(Type.REQFRIEND, "1"));
				nm.setv("ok", 1);
			} else {
				nm.setv("ok", 0);
			}
		} else {
			return Response.fail("对方已经是您的好友,不可重复添加哦!");
		}

		return nm;
	}

	/**
	 * 检查是否已经是好友
	 * 
	 * @param me
	 * @param uid
	 * @return
	 */
	public boolean isFirend(int me, int uid) {
		List<Friends> query = dao.query(Friends.class, Cnd.where(Friends.ME, "=", me).and(Friends.FRIEND, "=", uid));
		if (query == null || query.size() == 0) {// 不是则返回true
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 同意添加
	 * 
	 * @param me
	 *            我的id
	 * @param uid
	 *            对方的id
	 * @param group
	 *            我要添加到的分组id
	 * @param from_group
	 *            对方要添加到的分组id
	 * @return
	 */
	@At
	public Object addFridend(@Attr("me") int me, @Param("uid") int uid, @Param("group") int group,
			@Param("from_group") int from_group, @Param("msgid") int msgid) {
		NutMap nm = new NutMap();

		// 将对方添加成我的好友
		int id = userDao.addFriend(me, uid, group);
		// 将我添加成对方的好友
		int i = userDao.addFriend(uid, me, from_group);
		System.out.println("加好友成功!");

		// 更新消息状态
		userDao.updateMsg(msgid, 3); // 更新状态为已同意

		// 给对方发送一个添加成功的消息
		Message msg = new Message();
		msg.setContent("成功添加对方为好友!");
		msg.setUid(uid);
		msg.setFrom(me);
		msg.setFrom_group(from_group);
		msg.setType(3);
		msg.setRead(1);
		msg.setTime(new Date());
		msg.setRemark("");
		Message i2 = dao.insert(msg);

		User fetch = dao.fetch(User.class, me);
		NutMap n = new NutMap();
		n.setv("user", fetch);
		n.setv("group", from_group);
		SocketUtils.sendByUserId(String.valueOf(uid),
				SocketMsgUtils.madeWsResponse(Type.REQFRIENDSUCCESS, Json.toJson(n)));

		nm.setv("code", 0);
		return nm;
	}

	/**
	 * 拒绝添加
	 * 
	 * @param me
	 * @param msgid
	 * @return
	 */
	@At
	public Object declineApply(@Attr("me") int me, @Param("msgid") int msgid, @Param("remark") String remark) {
		NutMap nm = new NutMap();
		// 更新消息状态
		userDao.updateMsg(msgid, 2);

		// 给对方发一条拒绝通知
		Message fetch = dao.fetch(Message.class, msgid);
		Message msg = new Message();
		msg.setType(2);
		msg.setContent("对方拒绝了您的请求消息");
		msg.setUid(fetch.getFrom());
		msg.setFrom(me);
		msg.setFrom_group(fetch.getFrom_group());
		msg.setRemark(remark);
		msg.setHref("");
		msg.setRead(0);
		msg.setTime(new Date());
		dao.insert(msg);

		// socket通知对方
		SocketUtils.sendByUserId(String.valueOf(fetch.getFrom()), SocketMsgUtils.madeWsResponse(Type.REQFRIEND, "1"));

		nm.setv("code", 0);
		return nm;
	}

	/**
	 * 上线
	 * 
	 * @param me
	 */
	@At
	public void online(@Attr("me") int me) {
		userDao.online(me);
	}

	/**
	 * 下线
	 * 
	 * @param me
	 */
	@At
	public void hide(@Attr("me") int me) {
		userDao.hide(me);
	}

	/**
	 * 修改签名
	 * 
	 * @param me
	 * @param sign
	 */
	@At
	public void updateSign(@Attr("me") int me, @Param("sign") String sign) {
		userDao.updateSign(me, sign);
	}

	/**
	 * 根据id获取用户信息,可用于查看在线状态
	 * 
	 * @param id
	 * @return
	 */
	@At
	public Object getUser(@Param("id") int id) {
		User user = userDao.findbyid(id);
		user.setPwd(null);
		return user;
	}

	/**
	 * 查询群成员
	 * 
	 * @return
	 */
	@At
	public Object getMembers(@Param("id") int fid) {
		List<User> members = userDao.getMembers(fid);
		if (members != null) {
			for (int i = 0; i < members.size(); i++) {
				members.get(i).setPwd(null);
			}
		}
		InitData id = new InitData();
		id.setCode(0);
		id.setMsg("");

		Map<String, Object> war = new HashMap<String, Object>();
		war.put("list", members);
		id.setData(war);
		return id;
	}

	/**
	 * 分页查询聊天记录
	 * 
	 * @param me
	 * @param pageNo
	 * @param pageSize
	 * @param toid
	 * @param type
	 * @return
	 */
	@At
	@Ok("json")
	public Object getOldMsgs(@Attr("me") int me, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize,
			@Param("toid") int toid, @Param("type") int type) {
		/*
		 * username: '纸飞机' ,id: 1 ,avatar:
		 * 'http://tva3.sinaimg.cn/crop.0.0.512.512.180/
		 * 8693225ajw8f2rt20ptykj20e80e8weu.jpg' ,timestamp: 1480897882000
		 * ,content: 'face[抱抱] face[心] 你好啊小美女'
		 */
		NutMap nm = chatMessageDao.pageMsg(pageNo, pageSize, me, toid, type);
		return nm;
	}

	/**
	 * Validate Data
	 * 
	 * @param user
	 * @param create
	 * @return
	 */
	protected String checkUser(User user, boolean create) {
		if (user == null) {
			return "空对象";
		}
		if (create) {
			if (Strings.isBlank(user.getUsername()) || Strings.isBlank(user.getPwd()))
				return "用户名/密码不能为空";
		} else {
			if (Strings.isBlank(user.getPwd()))
				return "密码不能为空";
		}
		// dao.update(User.class,org.nutz.dao.Chain.make("name","admin").add("age",15),Cnd.where("id","=",2));
		String passwd = user.getPwd().trim();
		/*
		 * if (6 > passwd.length() || passwd.length() > 12) { return
		 * "密码长度必须大于6位小于12位!"; }
		 */

		user.setPwd(passwd);
		/*
		 * if (create) { int count = dao.count(User.class, Cnd.where("username",
		 * "=", user.getUsername()));
		 * 
		 * if (count != 0) { if ("wendal".equalsIgnoreCase(user.getUsername()))
		 * { return "wendal大叔可直接登录的哦!\n账户:wendal\n密码:wendal\n(๑′ᴗ‵๑)"; } return
		 * "用户名已经存在"; } }
		 */
		if (user.getUsername() != null)
			user.setUsername(user.getUsername().trim());
		return null;
	}

	/**
	 * 分手...
	 * 
	 * @return
	 */
	@At
	public Object separate(@Param("id") int id, @Attr("me") int me) {
		int i = dao.clear(Friends.class, Cnd.where(Friends.ME, "=", me).and(Friends.FRIEND, "=", id));
		if (i > 0)
			return Response.ok();
		else
			return Response.fail();
	}

	/**
	 * 加群
	 * 
	 * @param userId
	 * @param groupId
	 * @param me
	 * @return
	 */
	@At
	public Object innerGroup(@Param("userId") int userId, @Param("groupId") int groupId, @Attr("me") int me,
			@Param("remark") String remark) {
		Flock group = dao.fetch(Flock.class, groupId);
		if (group == null) {
			return Response.fail("群组不存在!");
		}
		FlockRefUser user = dao.fetch(FlockRefUser.class,
				Cnd.where(FlockRefUser.UID, "=", userId).and(FlockRefUser.FID, "=", groupId));
		if (user != null) {
			return Response.fail("不可重复加群!");
		}

		Message msg = new Message();
		msg.setContent("请求加入" + group.getGroupname() + "群");
		msg.setUid(group.getUserid());// 发给群主呀
		msg.setFrom(userId);
		msg.setFrom_group(groupId);
		msg.setType(4);
		msg.setRemark(remark);
		msg.setHref("");
		msg.setRead(0);
		msg.setTime(new Timestamp(System.currentTimeMillis()));

		dao.clear(Message.TABLE_NAME, Cnd.where(Message.FROM, "=", userId).and(Message.UID, "=", group.getUserid())
				.and(Message.TYPE, "=", "4").and(Message.READ, "=", 0));

		dao.insert(msg);

		// Socket通知
		SocketUtils.sendByUserId(String.valueOf(group.getUserid()), SocketMsgUtils.madeWsResponse(Type.REQFRIEND, "1"));
		return Response.ok();
	}

	/***
	 * 同意加群请求
	 * 
	 * @param id
	 *            请求编号
	 * @param me
	 *            当前用户
	 * @return
	 */
	@At
	@POST
	public Object agreeGroup(@Param("msgId") int id, @Attr("me") int me) {
		Message fetch = dao.fetch(Message.class, id);
		if (fetch == null) {
			return Response.fail("请求消息不存在!");
		}

		Flock group = dao.fetch(Flock.class, fetch.getFrom_group());

		if (group == null) {
			return Response.fail("该群已解散或不存在!");
		}

		if (fetch.getUid() != me) {
			return Response.fail("您不是群主,操作失败!");
		}

		// 加群
		FlockRefUser fr = new FlockRefUser();
		fr.setFid(fetch.getFrom_group());
		fr.setUid(fetch.getFrom());
		dao.clear(FlockRefUser.class,
				Cnd.where(FlockRefUser.UID, "=", fetch.getFrom()).and(FlockRefUser.FID, "=", fetch.getFrom_group()));
		dao.insert(fr);

		// 消息已读
		fetch.setRead(1);
		fetch.setType(3);
		dao.updateIgnoreNull(fetch);

		// 给请求人发个加群成功的消息
		Message msg = new Message();
		msg.setContent("您已加入 " + group.getGroupname() + " 群!");
		msg.setUid(fetch.getFrom());
		msg.setFrom(me);
		msg.setFrom_group(fetch.getFrom_group());
		msg.setType(3);
		msg.setRead(0);
		msg.setTime(new Date());
		msg.setRemark("");
		Message i2 = dao.insert(msg);

		// Socket通知对方
		NutMap nm = new NutMap();
		nm.setv("type", "group");
		nm.setv("avatar", group.getAvatar());
		nm.setv("groupname", group.getGroupname());
		nm.setv("id", group.getId());
		SocketUtils.sendByUserId(String.valueOf(fetch.getFrom()),
				SocketMsgUtils.madeWsResponse(Type.JOIN_GROUP_REQ, Json.toJson(nm)));
		return Response.ok();
	}

	/***
	 * 同意加群请求
	 * 
	 * @param id
	 *            请求编号
	 * @param me
	 *            当前用户
	 * @return
	 */
	@At
	@POST
	public Object refuseGroup(@Param("msgId") int id, @Attr("me") int me, @Param("remark") String remark) {
		Message fetch = dao.fetch(Message.class, id);
		if (fetch == null) {
			return Response.fail("请求消息不存在!");
		}

		Flock group = dao.fetch(Flock.class, fetch.getFrom_group());
		if (group == null) {
			return Response.fail("该群已解散或不存在!");
		}
		if (fetch.getUid() != me) {
			return Response.fail("您不是群主,操作失败!");
		}

		// 更新消息状态
		userDao.updateMsg(id, 2);

		// 给对方发一条拒绝通知
		Message msg = new Message();
		msg.setType(2);
		msg.setContent("拒绝了您的加群请求!");
		msg.setUid(fetch.getFrom());
		msg.setFrom(me);
		msg.setFrom_group(fetch.getFrom_group());
		msg.setRemark(remark);
		msg.setHref("");
		msg.setRead(0);
		msg.setTime(new Date());
		dao.insert(msg);

		// socket通知对方
		SocketUtils.sendByUserId(String.valueOf(fetch.getFrom()), SocketMsgUtils.madeWsResponse(Type.REQFRIEND, "1"));

		return Response.ok();
	}

	Log log = Logs.get();

	/**
	 * 随机推荐几个人和群
	 * 
	 * @return
	 */
	@At
	// @Ok("json:{actived:'username|groupname'}")
	public Object random() {
		NutMap re = new NutMap();
		List<User> list1 = dao.query(User.class, Cnd.wrap("ORDER BY RAND()"), new Pager(1, 4));
		List<Flock> list2 = dao.query(Flock.class, Cnd.wrap("ORDER BY RAND()"), new Pager(1, 4));
		for (int i = 0; i < list1.size(); i++) {
			list1.get(i).setPwd(null);
		}
		re.put("friend", list1);
		re.put("group", list2);
		return re;
	}

	@At("/login")
	@Ok("fm:/login")
	public Object login() {
		return "";
	}

	/**
	 * 创建分组
	 * 
	 * name 分组名
	 * 
	 * @return
	 */
	@At
	public Object addGroup(@Attr("me") int me, @Param("groupname") String name) {
		NutMap re = new NutMap();
		if (name != null) {
			int id = userDao.addGroup(me, name);

			Map<String, Object> map = new NutMap();
			map.put("id", id);
			map.put("name", name);
			re.setv("ok", true).setv("msg", "创建成功!").setv("data", map);
		} else {
			re.setv("ok", true).setv("msg", "创建失败!");
		}
		return re;
	}

	/**
	 * 修改分组名称
	 * 
	 * name 分组名
	 * 
	 * @return
	 */
	@At
	public Object updateGroup(@Param("targetIndex") int index, @Attr("me") int me,
			@Param("groupname") String groupname) {
		List<Group> groups = userDao.getGroup(me);
		if (groups == null) {
			return Response.fail("操作失败!");
		}
		if (groups.size() < index + 1) {
			return Response.fail("操作失败!");
		}
		Group group = groups.get(index);
		if (group == null) {
			return Response.fail("操作失败!");
		}
		dao.update("c_group", Chain.make(Group.GEOUPNAME, groupname), Cnd.where(Group.ID, "=", group.getId()));
		return Response.ok();

	}

	/**
	 * 删除分组
	 * 
	 * name 分组名
	 * 
	 * @return
	 */
	@At
	public Object deleteGroup(@Param("targetIndex") int index, @Attr("me") int me, @Attr("username") String username) {
		List<Group> groups = userDao.getGroup(me);
		if (groups == null) {
			return Response.fail("操作失败!");
		}
		if (groups.size() < index + 1) {
			return Response.fail("操作失败!");
		}
		Group group = groups.get(index);
		if (group == null) {
			return Response.fail("操作失败!");
		}
		List<Friends> friends = dao.query(Friends.class, Cnd.where(Friends.GROUPID, "=", group.getId()));
		dao.delete(group);
		if (friends != null) {

			dao.clear(Friends.class, Cnd.where(Friends.GROUPID, "=", group.getId()));
			for (int i = 0; i < friends.size(); i++) {

				Friends friend = friends.get(i);
				dao.clear(Friends.class,
						Cnd.where(Friends.ME, "=", friend.getFriend()).and(Friends.FRIEND, "=", friend.getMe()));
				chatMessageDao.sendDelFriendMsg("好友:(" + username + ")已将你删除", friend.getFriend(), me);
				// socket通知删除对方好友
				SocketUtils.sendByUserId(String.valueOf(friend.getFriend()),
						SocketMsgUtils.madeWsResponse(Type.DELFRIEND, Json.toJson(friend)));
			}
		}

		return Response.ok();

	}

	/**
	 * 删除好友
	 * 
	 * name 分组名
	 * 
	 * @return
	 */
	@At
	public Object deleteFriend(@Param("uid") int uid, @Attr("me") int me, @Attr("username") String username) {
		Friends friend = dao.fetch(Friends.class, Cnd.where(Friends.ME, "=", me).and(Friends.FRIEND, "=", uid));
		if (friend == null) {
			return Response.fail();
		}
		dao.clear(Friends.class,
				Cnd.where(Friends.ME, "=", friend.getMe()).and(Friends.FRIEND, "=", friend.getFriend()));
		dao.clear(Friends.class,
				Cnd.where(Friends.ME, "=", friend.getFriend()).and(Friends.FRIEND, "=", friend.getMe()));
		chatMessageDao.sendDelFriendMsg("好友:(" + username + ")已将你删除", uid, me);
		// socket通知删除对方好友
		SocketUtils.sendByUserId(String.valueOf(friend.getFriend()),
				SocketMsgUtils.madeWsResponse(Type.DELFRIEND, Json.toJson(friend)));
		return Response.ok();

	}

	/**
	 * 退出群组
	 * 
	 * name 分组名
	 * 
	 * @return
	 */
	@At
	public Object exitGroup(@Param("fid") int fid, @Attr("me") int me, @Attr("username") String username) {
		Flock flock = dao.fetch(Flock.class, fid);
		if (flock == null) {
			return Response.fail();
		}
		FlockRefUser flockRefUser = dao.fetch(FlockRefUser.class,
				Cnd.where(FlockRefUser.FID, "=", fid).and(FlockRefUser.UID, "=", me));
		if (flockRefUser == null) {
			return Response.fail();
		}
		if (!(flock.getUserid() == me)) {
			// 发送退出群消息
			chatMessageDao.sendQuitGroupMsg("用户:(" + username + ")已退出群(" + flock.getGroupname() + ")", fid,
					flock.getUserid(), me);
			SocketUtils.sendByUserId(String.valueOf(flock.getUserid()),
					SocketMsgUtils.madeWsResponse(Type.QUIT_GROUP_RESP, Json.toJson(flock)));
			// 退出群
			dao.clear(FlockRefUser.class, Cnd.where(FlockRefUser.FID, "=", fid).and(FlockRefUser.UID, "=", me));
			SocketUtils.sendByUserId(String.valueOf(me),
					SocketMsgUtils.madeWsResponse(Type.QUIT_GROUP_RESP, Json.toJson(flock)));
		} else {
			// 发送解散群消息
			chatMessageDao.sendClearGroupMsg("群:(" + flock.getGroupname() + ")已被群主解散", fid, me);
			// 解散群
			dao.clear(FlockRefUser.class, Cnd.where(FlockRefUser.FID, "=", fid));
			dao.clear(Flock.class, Cnd.where(Flock.ID, "=", fid));
			SocketUtils.sendByGroup(String.valueOf(fid),
					SocketMsgUtils.madeWsResponse(Type.QUIT_GROUP_RESP, Json.toJson(flock)));
		}
		return Response.ok();
	}

	/**
	 * 创建群组
	 * 
	 * groupname 组名 avatar 头像 des 描述
	 * 
	 * @return
	 */
	@At
	public Object createGroup(@Param("groupname") String groupname, @Param("avatar") String avatar,
			@Param("des") String des, @Attr("me") int me) {
		Flock f1 = new Flock();
		f1.setUserid(me);
		f1.setGroupname(groupname);
		f1.setAvatar(avatar);
		f1.setDes(des);
		dao.insert(f1);
		// 初始化群成员
		FlockRefUser fr1 = new FlockRefUser();
		fr1.setFid(f1.getId());
		fr1.setUid(me);
		dao.insert(fr1);
		SocketUtils.sendByUserId(String.valueOf(me),
				SocketMsgUtils.madeWsResponse(Type.JOIN_GROUP_REQ, Json.toJson(f1)));
		return Response.ok().setv("data", f1);
	}

	// @At
	// @Ok("fm:/user/hello")
	// public Object hello(){
	// return new NutMap().setv("hello","world");
	// }

	/**
	 * 注册
	 * 
	 * @param user
	 * @param session
	 * @return
	 */
	@At
	@POST
	public Object connect(@Param("..") User user, HttpSession session, HttpServletResponse response) {
		String msg = checkUser(user, true);
		if (msg != null) {
			return Response.fail().setv("msg", msg);
		}
		String account = user.getUsername();
		if (user.getPwd() == null || !user.getPwd().equalsIgnoreCase(Md5.getMD5("$#@!" + account))) {
			msg = "签名错误";
			return Response.fail().setv("msg", msg);
		}
		RcUser rcUser = dao.fetch(RcUser.class, Cnd.where(RcUser.ACCOUNT, "=", account));
		if (rcUser == null) {
			msg = "非法用户";
			return Response.fail().setv("msg", msg);
		}
		user.setPwd(rcUser.getPassword());
		if (dao.fetch(User.class, Cnd.where(User.USERNAME, "=", account)) == null) {
			return registry(user, session);
		} else {
			return login(user, session);
		}

	}
}
